package com.ijava.base.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.ijava.base.dao.MenuDAO;
import com.ijava.base.domain.Menu;
import com.ijava.base.service.MenuService;

public class MenuServiceImpl implements MenuService {
	Logger logger = LoggerFactory.getLogger(MenuServiceImpl.class);
	private MenuDAO dao;

	public MenuDAO getDao() {
		return dao;
	}

	public void setDao(MenuDAO dao) {
		this.dao = dao;
	}

	public void save(Menu menu) throws Exception {
		dao.save(menu);
	}

	public void remove(String id) throws Exception {
		dao.remove(id);
	}

	public void mutilRemove(Menu menu) throws Exception {
		try {
			String[] keys = menu.getKeys();
			if (null == keys)
				return;
			for (int i = 0; i < keys.length; i++) {
				remove(keys[i]);
			}
		} catch (Exception e) {
			logger.error("", e);
			throw e;
		}
	}

	public void update(Menu menu) throws Exception {
		dao.update(menu);
	}

	public void merge(Menu menu) throws Exception {
		dao.merge(menu);
	}

	public Menu get(String id) throws Exception {
		return dao.get(id);
	}

	public List find(Menu menu) throws Exception {
		try {
			List paramList = new ArrayList();
			StringBuffer condition = new StringBuffer(64);
			if (StringUtils.hasLength(menu.getMenuName())) {
				condition.append(" where menuName like ?");
				paramList.add("%" + menu.getMenuName() + "%");
			}

			String queryString = "from " + Menu.class.getName()
					+ condition.toString();
			return dao.find(queryString, paramList);
		} catch (Exception e) {
			logger.error("", e);
			throw e;
		}
	}

	public List findWithPaging(Menu menu) throws Exception {
		try {
			int firstRow = (menu.getPageNo() - 1) * menu.getPageSize();
			int maxRow = menu.getPageSize();
			List paramList = new ArrayList();
			StringBuffer condition = new StringBuffer(" where 1 = 1");
			if (StringUtils.hasLength(menu.getMenuName())) {
				condition.append(" and menuName like ?");
				paramList.add("%" + menu.getMenuName() + "%");
			}
			if (StringUtils.hasLength(menu.getModelId())) {
				condition.append(" and modelId = ?");
				paramList.add(menu.getModelId());
			}
			List list = dao.find("select count(*) from " + Menu.class.getName()
					+ condition.toString(), paramList);
			menu.setTotal(Integer.parseInt(list.get(0).toString()));

			String queryString = "from " + Menu.class.getName()
					+ condition.toString();
			return dao.findWithPaging(queryString, paramList, firstRow, maxRow);

		} catch (Exception e) {
			logger.error("", e);
			throw e;
		}
	}
	
	public int getMaxRowOrder(Menu menu) throws Exception{
		int rowOrder = 1;
		try {
			List paramList = new ArrayList();
			StringBuffer condition = new StringBuffer();
			if (StringUtils.hasLength(menu.getModelId())) {
				condition.append(" where modelId = ?");
				paramList.add(menu.getModelId());
			}
			List list = dao.find("select max(m.rowOrder) from " + Menu.class.getName() +
					" m"
					+ condition.toString(), paramList);
			if(list.size() == 1 && null != list.get(0)){
				rowOrder = Integer.parseInt(list.get(0).toString()) + 1;
			}
		} catch (Exception e) {
			logger.error("", e);
			throw e;
		}
		return rowOrder;
	}

}
